package stack_queue;

/**
 * Created with IntelliJ IDEA.
 * Description: 循环队列
 * User: 王圆豪
 * Date: 2024-08-15
 * Time: 20:46
 */
class Main{
    public static void main(String[] args) {
        CirQueue cirQueue = new CirQueue();
        cirQueue.push(12);
        cirQueue.push(23);
        cirQueue.push(34);
        cirQueue.push(45);
        cirQueue.push(56);
        cirQueue.push(67);
        cirQueue.push(78);
        cirQueue.pop();
        cirQueue.peek();
        for (int i = cirQueue.first; i < cirQueue.last; i++) {
            System.out.println(cirQueue.elem[i] + " ");
        }
    }
}

public class CirQueue {
    int first;
    int last;
    int[] elem;

    public CirQueue() {
        elem = new int[8];
    }

    public void push(int val) {
        if (isFull()) {
            return;
        }
        elem[last % 8] = val;
        last++;
    }

    public int pop() {
        if (isEmpty()) {
            return -1;
        }
        int k = elem[first];
        first = (first + 1) % 8;
        return k;
    }

    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return elem[first];
    }

    public boolean isEmpty() {
        return last == first;
    }

    public boolean isFull() {
        return (last + 1) % 8 == first;
    }
}
